﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using DevExpress.XtraBars;
using UniMana.Data;
using UniMana.Utils.Helper;

namespace UniMana.Win.Personnel
{
    public partial class FrmPersonelList_SubInfo : DevExpress.XtraEditors.XtraForm
    {
        DBMain m_data = new DBMain();
        public Personnel_PersonnelInfo m_personelInfo = new Personnel_PersonnelInfo();//;
        public bool m_Flag = true;
        private bool m_bType = false;

        private string m_Rule = UniMana.Utils.Configs.RuleConfig.LECTUREINFO;
        public FrmPersonelList_SubInfo()
        {
            if (!SysUtils.CheckAccessByRule(m_Rule))
            {
                return;
            }
            InitializeComponent();
            SysUtils.SetRuleToXucBar(xucBar1, this.m_Rule);
        }
        #region [Utils]
        /// <summary>
        /// Nạp dữ liệu
        /// </summary>
        public void LoadData()
        {
            #region [Drop-down]
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments);
            lueCatalogue_Degree.GetData(m_data.m_DCReadOnly.Catalogue_Degrees);
            lueCatalogue_PersonnelStatus.GetData(m_data.m_DCReadOnly.Catalogue_PersonnelStatus);
            lueCatalogue_Position.GetData(m_data.m_DCReadOnly.Catalogue_Positions);
            if (lueCatalogue_Position.GetIntValue() == 17)
            {
                //luePersonnel_PersonnelType.Enabled = true;
                luePersonnel_PersonnelType.GetData(m_data.m_DCReadOnly.Personnel_PersonnelTypes);
            }
            else
            {
                luePersonnel_PersonnelType.GetData(m_data.m_DCReadOnly.Personnel_PersonnelTypes);
                //luePersonnel_PersonnelType.Enabled = false;
            }

            lueCatalogue_Nation.GetData(m_data.m_DCReadOnly.Catalogue_Nations);
            lueCatalogue_City.GetData(m_data.m_DCReadOnly.Catalogue_Cities);
            lueCatalogue_District.GetData(m_data.m_DCReadOnly.Catalogue_Districts);
            lueCatalogue_Ethnic.GetData(m_data.m_DCReadOnly.Catalogue_Ethnics);
            lueCatalogue_Religion.GetData(m_data.m_DCReadOnly.Catalogue_Religions);
            lueCatalogue_Politics.GetData(m_data.m_DCReadOnly.Catalogue_Politics);
            #endregion
            LoadBindingSoureToLcg();
        }
        /// <summary>
        /// Khởi tạo sự kiện
        /// </summary>
        public void InitControlEvent()
        {
            //////xucBar.btnAddNew_Click += new xucBar.ButtonClickHandler(this.btnSave_AddNew_Click);
            //////xucBar.btnSave_Click += new xucBar.ButtonClickHandler(this.btnSave_Close_Click);
            //////xucBar.btnPrint_Click += new xucBar.ButtonClickHandler(this.btnPrint_Click);
        }
        /// <summary>
        /// LoadOnceRecord
        /// </summary>
        public void LoadBindingSoureToLcg()
        {
            try
            {
                lcgInfo.LoadOneRecord(m_personelInfo.m_bds);
                lcgPersonalInfo.LoadOneRecord(m_personelInfo.m_bds);
                lcgContactInfo.LoadOneRecord(m_personelInfo.m_bds);
                lcgNote.LoadOneRecord(m_personelInfo.m_bds);
                lcgSignature.LoadOneRecord(m_personelInfo.m_bds);
                //Giới tính
                if ((m_personelInfo.m_bds.Current as Personnel_Personnel).Gender == false)
                {
                    ckeMale.Checked = true;
                    ckeFemale.Checked = false;
                }
                else
                {
                    ckeFemale.Checked = true;
                    ckeMale.Checked = false;
                }
            }
            catch { }
        }
        /// <summary>
        /// Lưu thông tin
        /// </summary>
        public void SaveAll()
        {
            //Lưu
            this.Validate();
            if (!m_bType)
            {
                m_personelInfo.Properties.Personnel_PersonnelTypeKey = 3;
            }
            //Giới tính
            if (ckeMale.Checked)
            {
                (m_personelInfo.m_bds.Current as Personnel_Personnel).Gender = false; //Nam
            }
            else
            {
                (m_personelInfo.m_bds.Current as Personnel_Personnel).Gender = true; //Nữ 
            }
            m_personelInfo.Save();
        }
        #endregion
        #region [XucBar - Events]
        /// <summary>
        /// Nút Lưu và thêm mới
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_AddNew_Click(object sender, ItemClickEventArgs e)
        {

            if (!SysUtils.CheckSaveByRule(this.m_Rule))
            {
                return;
            }
            if (dvpPersonel.Validate())
            {
                try
                {
                    //MessageBox.Show(luePersonnel_PersonnelType.EditValue.ToString());
                    if (m_Flag == false)
                    {
                        this.SaveAll();
                        Guid newKey = Guid.NewGuid();
                        m_personelInfo = new Personnel_PersonnelInfo(newKey);
                        LoadBindingSoureToLcg();
                    }
                    else
                    {
                        if (m_personelInfo.CheckID(txtID.Text) == false)
                        {
                            this.SaveAll();
                            Guid newKey = Guid.NewGuid();
                            m_personelInfo = new Personnel_PersonnelInfo(newKey);
                            LoadBindingSoureToLcg();
                        }
                        else
                        {
                            XtraMessageBox.Show("Mã nhân viên đã tồn tại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
                m_Flag = true;
                txtID.Enabled = true;
            }
        }
        /// <summary>
        /// Lưu và đóng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Close_Click(object sender, ItemClickEventArgs e)
        {

            if (!SysUtils.CheckSaveByRule(this.m_Rule))
            {
                return;
            }

            if (dvpPersonel.Validate())
            {
                if (m_Flag == false)
                {
                    this.SaveAll();
                    this.Close();
                }
                else
                {
                    if (m_personelInfo.CheckID(txtID.Text) == false)
                    {
                        this.SaveAll();
                        this.Close();
                    }
                    else
                    {
                        XtraMessageBox.Show("Mã nhân viên đã tồn tại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                ////xucBar.dlg.Close();
            }
        }
        /// <summary>
        /// Nút In
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, ItemClickEventArgs e)
        {

            if (!SysUtils.CheckPrintByRule(this.m_Rule))
            {
                return;
            }
            //bds data = new bds();
            //data.DataSource = m_personelInfo.m_bds;
            Guid personelKey = Guid.Parse(m_personelInfo.Properties.Personnel_PersonnelKey.ToString());
            RptPersonel rpt = new RptPersonel(personelKey);
            rpt.ShowPreview();
            #region [old]
            //string name = txtLastName.Text +" "+ txtName.Text;
            //string gender="";
            //if(ckeFemale.Checked)
            //{
            //    gender ="Nữ";
            //}
            //if(ckeMale.Checked)
            //{
            //    gender = "Nam";
            //}
            //RptPersonel rpt = new RptPersonel(txtID.Text, name, txtIdentityNo.Text, dteBirthday.Text, gender,
            //    txtPlaceOfBirth.Text, lueCatalogue_Politics.Text, txtCellPhone.Text, txtHomePhone.Text,
            //    txtAddr.Text, txtTempAddr.Text, txtEmail.Text, dteBeginDateWork.Text, mneNotes.Text,
            //    lueCatalogue_Nation.Text, lueCatalogue_City.Text, lueCatalogue_District.Text,
            //    lueCatalogue_Ethnic.Text, lueCatalogue_Religion.Text, lueCatalogue_Politics.Text,
            //    lueCatalogue_Degree.Text, lueCatalogue_Department.Text, lueCatalogue_Position.Text, dtePoliticsJoinDate.Text);
            //rpt.ShowPreviewDialog();
            #endregion
        }
        #endregion
        #region [Form - Events]
        /// <summary>
        /// PictureEdit được Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptePicture_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog odg = new OpenFileDialog();
                odg.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
                odg.ShowDialog(this);
                string imageFile = odg.FileName;

                if (imageFile != string.Empty)
                {
                    Image img = Image.FromFile(imageFile);

                    ptePicture.Image = Image.FromFile(imageFile);
                    img = img.GetThumbnailImage(240, 480, null, new System.IntPtr());
                    ptePicture.Image = img;
                    m_personelInfo.m_bds.EndEdit();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        private void pteSignature_Click_1(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog odg = new OpenFileDialog();
                odg.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
                odg.ShowDialog(this);
                string imageFile = odg.FileName;
                if (imageFile != string.Empty)
                {
                    Image img = Image.FromFile(imageFile);
                    pteSignature.Image = Image.FromFile(imageFile);
                    img = img.GetThumbnailImage(240, 480, null, new System.IntPtr());
                    pteSignature.Image = img;
                    m_personelInfo.m_bds.EndEdit();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        private void FrmPersonelInfo_Load(object sender, EventArgs e)
        {
            LoadData();
            InitControlEvent();
            if (m_Flag == false)
            {
                txtID.Enabled = false;
                if (lueCatalogue_Position.GetIntValue() == 17)
                {
                    lbcRegistSubject.Visible = true;
                }
                else
                {
                    //lbcRegistSubject.Enabled = false;
                    lbcRegistSubject.Visible = false;
                }
            }
            else
            {
                txtID.Enabled = true;
                ////xucBar.btnPrint.Enabled = false;
            }
        }
        private void lueCatalogue_Position_EditValueChanged(object sender, EventArgs e)
        {
            //if (lueCatalogue_Position.GetIntValue() == 17)
            //{
            //    lbcRegistSubject.Visible = true;
            //    luePersonnel_PersonnelType.Enabled = true;
            //    m_bType = true;
            //}
            //else
            //{
            //    lbcRegistSubject.Visible = false;
            //    luePersonnel_PersonnelType.Enabled = false;
            //    luePersonnel_PersonnelType.EditValue = null;
            //    m_bType = false;
            //    //this.m_personelInfo.Properties.Personnel_PersonnelTypeKey = null;
            //    //luePersonnel_PersonnelType.GetData(m_data.m_DCReadOnly.Personnel_PersonnelTypes);
            //}
        }
        private void lbcRegistSubject_Click(object sender, EventArgs e)
        {
            //XtraMessageBox.Show("Giảng viên!!!", "Chức vụ");
            ////FrmPersonnelRegistSubject f = new FrmPersonnelRegistSubject(lueCatalogue_Department.GetIntValue(), txtID.Text);
            ////f.ShowDialog();
        }
        private void lbcRegistSubject_MouseHover(object sender, EventArgs e)
        {
            this.lbcRegistSubject.Appearance.Font =new  System.Drawing.Font("Tahoma", 9F, ((System.Drawing.FontStyle)(((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)
                            | System.Drawing.FontStyle.Underline))));
        }
        private void lbcRegistSubject_MouseLeave(object sender, EventArgs e)
        {
            this.lbcRegistSubject.Appearance.Font = new System.Drawing.Font("Tahoma", 9F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold))));
        }
        private void lueCatalogue_City_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                lueCatalogue_District.GetData(m_data.m_DCReadOnly.Catalogue_Districts.Where(p => p.Catalogue_CityKey == lueCatalogue_City.GetIntValue()));
            }
            catch { }
        }
        private void ckeTeach_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeTeach.Checked == true)
            {
                lbcRegistSubject.Visible = true;
                //luePersonnel_PersonnelType.Enabled = true;
                m_bType = true;
            }
            else
            {
                lbcRegistSubject.Visible = false;
                //luePersonnel_PersonnelType.Enabled = false;
                //luePersonnel_PersonnelType.EditValue = null;
                m_bType = false;
                //this.m_personelInfo.Properties.Personnel_PersonnelTypeKey = null;
                //luePersonnel_PersonnelType.GetData(m_data.m_DCReadOnly.Personnel_PersonnelTypes);
            }
        }
        #endregion       
    }
}